package com.wcar.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wcar.common.entity.UserAddress;
import com.wcar.common.vo.Result;
import com.wcar.user.dao.UserAddressDao;
import com.wcar.user.service.UserAddressService;

import org.apache.tomcat.jni.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: four-wcar-idea
 * @description:
 * @author: 吴朝阳
 * @create: 2024-05-31 17:29
 **/
@Service
public class UserAddressServiceImpl implements UserAddressService {
    @Autowired
    private UserAddressDao userAddressDao;
    @Override
    public Result<List<UserAddress>> selectAddress(Integer userId) {
        QueryWrapper<UserAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<UserAddress> userAddresses = userAddressDao.selectList(wrapper);
        return new Result<>(200,"查询地址成功",userAddresses);
    }

    @Override
    public Result<UserAddress> selectOneAddress(UserAddress userAddress) {
        if(userAddress.getFlag().equals("0")){
            QueryWrapper<UserAddress> wrapper = new QueryWrapper<>();
            wrapper.eq("flag",0);
            wrapper.eq("user_id",userAddress.getUserId());
            UserAddress address = userAddressDao.selectOne(wrapper);
            address.setFlag("1");
            userAddressDao.updateById(address);
        }
        userAddressDao.updateById(userAddress);
        return new Result<>(200,"修改地址成功",userAddress);
    }

    @Override
    public Result<UserAddress> selectOne(Integer id) {
        QueryWrapper<UserAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("address_id",id);
        UserAddress userAddress = userAddressDao.selectOne(wrapper);
        return new Result<>(200,"查询成功",userAddress);
    }

    @Override
    public Result<List<UserAddress>> selectCity(String country, String city, String province) {
        QueryWrapper<UserAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("district", country);
        wrapper.eq("city", city);
        wrapper.eq("province", province);
        List<UserAddress> userAddresses = userAddressDao.selectList(wrapper);
        return new Result<> (200,"查询省份成功",userAddresses);
    }

    @Override
    public Result<UserAddress> insertUserAddress(UserAddress userAddress) {
        QueryWrapper<UserAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("flag",0);
        wrapper.eq("user_id",userAddress.getUserId());
        UserAddress address = userAddressDao.selectOne(wrapper);
        if(address==null){
            userAddressDao.updateById(userAddress);
        }else{
            userAddress.setFlag("1");
            userAddressDao.updateById(userAddress);
        }
        userAddressDao.insert(userAddress);
        return new Result<>(200,"添加成功",userAddress);
    }

    @Override
    public Result<UserAddress> deleteUserAddress(UserAddress addressId) {
        QueryWrapper<UserAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("address_id",addressId.getAddressId());
        int i = userAddressDao.delete(wrapper);
        if (i>0){
            return new Result<>(200,"删除成功",null);
        }
        return new Result<>(200,"删除失败",null);

    }
}
